package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.SubOptionVO;
import com.gibs.peixunapi.dao.SubOptionDao;
import com.gibs.peixunapi.dao.SubjectHubDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.excepiton.BaseException;
import com.gibs.peixunapi.model.SubOption;
import com.gibs.peixunapi.model.SubjectHub;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.SubOptionService;
import com.gibs.peixunapi.utils.ConverterUtil;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SubOptionServiceImpl implements SubOptionService {

    @Autowired
    private SubOptionDao subOptionDao;
    @Autowired
    private SubjectHubDao subjectHubDao;

    @Override
    public Result create(List<SubOptionVO> objectList, SubjectHub subjectHub) {
        if (objectList.size() < 1 || subjectHub == null) {
            throw new BaseException(ResultEnum.PARAM_ERROR);
        }
        List<SubOption> subOptionList = new ArrayList<>(objectList.size());
        for (SubOptionVO subOptionVO : objectList) {
            SubOption subOption = new SubOption();
            ConverterUtil.copyNotBlankProperties(subOptionVO, subOption);
            subOption.setStatus(1);
            subOption.setCreateTime(new Date());
            subOption.setSubjectHub(subjectHub);
            subOptionList.add(subOption);
        }
        try {
            subOptionDao.saveAll(subOptionList);
            return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
        } catch (Exception e) {
            throw new BaseException(ResultEnum.CREATE_FAIL, e);
        }
    }

    @Override
    @Modifying(clearAutomatically = true)
    public Result save(List<SubOptionVO> objectList) {
        List<SubOption> subOptionList = new ArrayList<>(objectList.size());
        for (SubOptionVO subOptionVO : objectList) {
            SubOption subOption = subOptionDao.findById(subOptionVO.getId()).orElseThrow(EntityNotFoundException::new);
            ConverterUtil.copyDifferentProperties(subOptionVO, subOption);
            subOptionList.add(subOption);
        }
        try {
            subOptionDao.saveAll(subOptionList);
            return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            throw new BaseException(ResultEnum.UPDATE_FAIL, e);
        }
    }

    @Override
    public Result delete(Integer id) {
        try {
            subOptionDao.deleteById(id);
            return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException(ResultEnum.DELETE_FAIL,e);
        }
    }

    @Override
    public Result get(Integer id) {
        try {

            return ResultUtil.success(ResultEnum.FIND_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getList(Integer subjectHubId) {
        try {
            List<SubOption> subOptionList = subOptionDao.findSubOptionsBySubjectHubId(subjectHubId);
            if (subOptionList.isEmpty()){
                throw new EntityNotFoundException();
            }
            List<SubOptionVO> subOptionVOList = new ArrayList<>(subOptionList.size());
            subOptionList.forEach(subOption -> {
                SubOptionVO subOptionVO = new SubOptionVO();
                ConverterUtil.copyProperties(subOption, subOptionVO);
                subOptionVOList.add(subOptionVO);
            });
            return ResultUtil.success(ResultEnum.FIND_SUCCESS,subOptionVOList);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException(ResultEnum.DATA_NOT_FIND);
        }
    }


    @Override
    public Result create(List<SubOptionVO> objectList, Integer subjectHubId) {
        if (objectList.size() < 1 || subjectHubId == null) {
            throw new BaseException(ResultEnum.PARAM_ERROR);
        }

        SubjectHub subjectHub = subjectHubDao.findById(subjectHubId).orElseThrow(EntityNotFoundException::new);

        List<SubOption> subOptionList = new ArrayList<>(objectList.size());
        for (SubOptionVO subOptionVO : objectList) {
            SubOption subOption = new SubOption();
            ConverterUtil.copyNotBlankProperties(subOptionVO, subOption);
            subOption.setStatus(1);
            subOption.setCreateTime(new Date());
            subOption.setSubjectHub(subjectHub);
            subOptionList.add(subOption);
        }
        try {
            subOptionDao.saveAll(subOptionList);
            return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
        } catch (Exception e) {
            throw new BaseException(ResultEnum.CREATE_FAIL, e);
        }
    }

    @Override
    public List<SubOptionVO> getSubOptions(Integer subjectHubId) {
            List<SubOption> subOptionList = subOptionDao.findSubOptionsBySubjectHubId(subjectHubId);
            if (subOptionList.isEmpty()){
                throw new EntityNotFoundException();
            }
            List<SubOptionVO> subOptionVOList = new ArrayList<>(subOptionList.size());
            subOptionList.forEach(subOption -> {
                SubOptionVO subOptionVO = new SubOptionVO();
                ConverterUtil.copyProperties(subOption, subOptionVO);
                subOptionVOList.add(subOptionVO);
            });
            return subOptionVOList;
    }

}

